package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.WorkOrderSupport;
import com.tongtech.system.service.IWorkOrderSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 工单支持人员信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/workOrderSupport")
public class WorkOrderSupportController extends BaseController
{
    @Autowired
    private IWorkOrderSupportService workOrderSupportService;

    /**
     * 获取工单支持人员列表
     */
    @RequiresPermissions("workOrderSupport:info:list")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrderSupport workOrderSupport)
    {
        startPage();
        List<WorkOrderSupport> list = workOrderSupportService.selectWorkOrderSupportList(workOrderSupport);
        return getDataTable(list);
    }

    /**
     * 根据工单支持人员编号获取详细信息
     */
    @RequiresPermissions("workOrderSupport:info:query")
    @GetMapping(value = "/{workOrderSupportId}")
    public AjaxResult getInfo(@PathVariable Long workOrderSupportId)
    {
        return success(workOrderSupportService.selectWorkOrderSupportById(workOrderSupportId));
    }

    /**
     * 新增工单支持人员
     */
    @RequiresPermissions("workOrderSupport:info:add")
    @Log(title = "工单支持人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkOrderSupport workOrderSupport)
    {
       if(Objects.nonNull(workOrderSupport)){
           workOrderSupport.setCreateBy(SecurityUtils.getUsername());
       }
        return toAjax(workOrderSupportService.insertWorkOrderSupport(workOrderSupport));
    }

    /**
     * 修改工单支持人员
     */
    @RequiresPermissions("workOrderSupport:info:edit")
    @Log(title = "工单支持人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WorkOrderSupport workOrderSupport)
    {
        if(Objects.nonNull(workOrderSupport)){
            workOrderSupport.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(workOrderSupportService.updateWorkOrderSupport(workOrderSupport));
    }

    /**
     * 删除工单支持人员
     */
    @RequiresPermissions("workOrderSupport:info:remove")
    @Log(title = "工单支持人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workOrderSupportId}")
    public AjaxResult remove(@PathVariable Long workOrderSupportId)
    {
        return toAjax(workOrderSupportService.deleteWorkOrderSupportById(workOrderSupportId));
    }

    /**
     * 删除工单支持人员
     */
    @RequiresPermissions("workOrderSupport:info:remove")
    @Log(title = "工单支持人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/byWorkOrderId/{workOrderId}")
    public AjaxResult byWorkOrderId(@PathVariable Long workOrderId)
    {
        return toAjax(workOrderSupportService.deleteWorkOrderSupportByWorkOrderId(workOrderId));
    }
}
